import java.util.*;

public class BinaryTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode(1);
        TreeNode B = new TreeNode(2);
        TreeNode C = new TreeNode(3);
        TreeNode D = new TreeNode(4);
        TreeNode E = new TreeNode(5);
        TreeNode F = new TreeNode(6);
        TreeNode G = new TreeNode(7);
        TreeNode H = new TreeNode(8);
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    public List<TreeNode> prevOrderToList(TreeNode root) {
        List<TreeNode> list = new ArrayList<>();
        // 前序遍历为顺序表
        if (root == null) {
            return list;
        }
        list.add(new TreeNode(root.val));
        list.addAll(prevOrderToList(root.left));
        list.addAll(prevOrderToList(root.right));
        return list;
    }

    public void prevOrder(TreeNode root) {
        // 前序遍历
        if (root == null) {
            return;
        }
        System.out.println(root.val + "");
        prevOrder(root.left);
        prevOrder(root.right);
    }

    public void inOrder(TreeNode root) {
        // 中序遍历
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.println(root.val + " ");
        inOrder(root.right);
    }

    public void postOrder(TreeNode root) {
        // 后序遍历
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root.val + " ");
    }

    public int nodeSize(TreeNode root) {
        // 获取树中节点的个数
        if (root == null)
            return 0;
        return nodeSize(root.left) + nodeSize(root.right) + 1;
    }

    public int getLeafNodeCount(TreeNode root) {
        // 获取叶子节点的个数
        if (root == null)
            return 0;
        if (root.left == null && root.right == null)
            return 1;
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    public int getKLevelNodeCount(TreeNode root, int k) {
        // 获取第K层节点的个数
        if (root == null)
            return 0;
        if (k == 1)
            return 1;
        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }

    public int getHeight(TreeNode root) {
        // 获取二叉树的高度
        if (root == null)
            return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return Math.max(leftHeight + 1, rightHeight + 1);
    }

    public TreeNode find(TreeNode root, int val) {
        // 检测值为value的元素是否存在
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode left = find(root.left, val);
        if (left != null) {
            return left;
        }
        TreeNode right = find(root.right, val);
        if (right != null) {
            return right;
        }
        return null;
    }

    public void levelOrder(TreeNode root) {
        //层序遍历
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

    public List<List<Integer>> levelOrder_ret(TreeNode root) {
        //层序遍历变形
        List<List<Integer>> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return list;
        }
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> level = new ArrayList<>();
            while (size != 0) {
                TreeNode cur = queue.poll();
                size--;
                level.add(cur.val);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            list.add(level);
        }
        return list;
    }

    public boolean isCompleteTree(TreeNode root) {
        // 判断一棵树是不是完全二叉树
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return true;
        }
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur == null) {
                break;
            }
            queue.offer(root.left);
            queue.offer(root.right);
        }
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                return false;
            }
        }
        return true;
    }

    public int i = 0;

    public TreeNode createTree(String str) {
        TreeNode root = null;
        if (str.charAt(i) != '#') {
            root = new TreeNode(str.charAt(i));
            root.left = createTree(str);
            root.right = createTree(str);
        }
        i++;
        return root;
    }

    public boolean haveNode(TreeNode root, TreeNode node) {
        if (root == null) {
            return false;
        }
        if (root == node) {
            return true;
        }
        boolean left = haveNode(root.left, node);
        boolean right = haveNode(root.right, node);
        return left || right;
    }

    public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q) {
        //最近公共祖先（LCA问题）法一
        if (root == null) {
            return null;
        }
        TreeNode leftRet = lowestCommonAncestor1(root.left, p, q);
        if (leftRet != null) {
            return leftRet;
        }
        TreeNode rightRet = lowestCommonAncestor1(root.right, p, q);
        if (rightRet != null) {
            return rightRet;
        }
        boolean haveP = haveNode(root, p);
        boolean haveQ = haveNode(root, q);
        if (haveP && haveQ) {
            return root;
        } else {
            return null;
        }
    }

    public boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
        if (root == null || node == null) {
            return false;
        }
        stack.push(root);
        if (root == node) {
            return true;
        }
        boolean flagLeft = getPath(root.left, node, stack);
        if (flagLeft) {
            return true;
        }
        boolean flagRight = getPath(root.right, node, stack);
        if (flagRight) {
            return true;
        }
        stack.pop();
        return false;
    }

    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        //最近公共祖先（LCA问题）法二
        if (root == null) {
            return null;
        }
        Stack<TreeNode> stackP = new Stack<>();
        Stack<TreeNode> stackQ = new Stack<>();
        getPath(root, p, stackP);
        getPath(root, q, stackQ);
        int sizeP = stackP.size();
        int sizeQ = stackQ.size();
        if (sizeP > sizeQ) {
            int i = sizeP - sizeQ;
            while (i != 0) {
                stackP.pop();
                i--;
            }
        } else {
            int i = sizeQ - sizeP;
            while (i != 0) {
                stackQ.pop();
                i--;
            }
        }
        while(!stackP.isEmpty() && !stackQ.isEmpty()) {
            TreeNode nodeP = stackP.pop();
            TreeNode nodeQ = stackQ.pop();
            if(nodeP.equals(nodeQ)) {
                return nodeP;
            }
        }
        return null;
    }
}
